{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "再看看序列 → list，tuple，str都是序列\n",
    "* 其中list是可变序列\n",
    "* tuple、str是不可变序列"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1、索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, [1, 2, 3, 4]]\n",
      "1 2\n",
      "[1, 2, 3, 4] 5\n"
     ]
    }
   ],
   "source": [
    "lst = [1,2,3,4,5,[1,2,3,4]]\n",
    "print(lst)\n",
    "print(lst[0],lst[1])\n",
    "print(lst[-1],lst[-2])\n",
    "# python索引从0开始\n",
    "# lst[-1]代表最后一个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('a', 'b', 'c', 'd', ('hello', 'world'))\n",
      "c ('hello', 'world')\n"
     ]
    }
   ],
   "source": [
    "tup = ('a','b','c','d',('hello','world'))\n",
    "print(tup)\n",
    "print(tup[2],tup[-1])\n",
    "# 元组索引和列表一样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "h l d\n"
     ]
    }
   ],
   "source": [
    "st = 'hello world'\n",
    "print(st[0],st[2],st[-1])\n",
    "# 字符串索引和列表一样"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2、切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3]\n",
      "[1, 2]\n",
      "[2, 3, 4, 5, [1, 2, 3, 4]]\n",
      "[2, 3, 4, 5]\n",
      "ello world\n",
      "hello wor\n",
      "('c', 'd', ('hello', 'world'))\n"
     ]
    }
   ],
   "source": [
    "# 切片用：表示\n",
    "print(lst[2:3])   # 索引2至索引3，但不包含索引3\n",
    "print(lst[:2])    # 从索引0至索引2，但不包含索引2\n",
    "print(lst[1:])    # 从索引1至最后一个元素，包含最后一个元素\n",
    "print(lst[1:-1])  # 从索引1至最后一个元素，不包含最后一个元素\n",
    "print(st[1:])\n",
    "print(st[:-2])\n",
    "print(tup[2:5])\n",
    "# 注意：切片是左闭右开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10, 30, 50, 70, 90]\n"
     ]
    }
   ],
   "source": [
    "# 什么是步长\n",
    "lst = [10,20,30,40,50,60,70,80,90]\n",
    "print(lst[::2])\n",
    "# lst[a:b:n] → 对于lst序列，从索引a至索引b，但不包含索引b，其中间隔为n（步长）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3、再来看看什么是“可变序列”和“不可变序列”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 100, 5]\n",
      "(3, 4, 5)\n",
      "345\n"
     ]
    }
   ],
   "source": [
    "# 修改序列的元素值\n",
    "lst = [3,4,5]\n",
    "tup = (3,4,5)\n",
    "st = '345'\n",
    "lst[1] = 100\n",
    "#tup[1] = 100\n",
    "#st[1] = 'h'\n",
    "print(lst)\n",
    "print(tup)\n",
    "print(st)\n",
    "# 可见 → 元组和字符串不能修改元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n",
      "[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, ['a', 'b']]\n",
      "------\n",
      "[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, ['a', 'b'], 'a', 'b']\n",
      "------\n",
      "[10, 20, 1000, 30, 40, 50, 60, 70, 80, 90, 100, ['a', 'b'], 'a', 'b']\n",
      "------\n",
      "[10, 20, 1000, 30, 40, 50, 60, 70, 80, 90, 100, ['a', 'b'], 'a', 'b'] 2640459676808\n",
      "[10, 20, 1000, 30, 40, 50, 60, 70, 80, 90, 100, ['a', 'b'], 'a', 'b'] 2640459676808\n",
      "[10, 20, 1000, 30, 40, 50, 60, 70, 80, 90, 100, ['a', 'b'], 'a', 'b'] 2640459676936\n"
     ]
    }
   ],
   "source": [
    "# 列表的常用方法、函数\n",
    "lst = [10,20,30,40,50,60,70,80,90]\n",
    "\n",
    "print(len(lst))  \n",
    "# len(): 求长度\n",
    "\n",
    "lst.append(100)\n",
    "lst.append(['a','b'])\n",
    "print(lst)\n",
    "print('------')\n",
    "# lst.append()：列表末尾添加新的元素\n",
    "\n",
    "lst.extend(['a','b'])\n",
    "print(lst)\n",
    "print('------')\n",
    "# lst.extend()：在列表末尾一次性追加另一个序列中的多个值（用新列表扩展原来的列表）\n",
    "\n",
    "lst.insert(2,1000)\n",
    "print(lst)\n",
    "print('------')\n",
    "# lst.insert()：这里是在索引2的位置，插入值1000\n",
    "\n",
    "lst2 = lst\n",
    "lst3 = lst.copy()\n",
    "print(lst,id(lst))\n",
    "print(lst2,id(lst2))\n",
    "print(lst3,id(lst3))\n",
    "# 复制列表，注意指向问题"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
